var form, table, tableInstance, laydate, $;

$(function () {
    initLayUI(function () {
        initButtons();
    })
});

function initLayUI(cb) {
    layui.use(['table', 'form', 'laydate', 'jquery'], function () {
        $ = layui.jquery;
        table = layui.table;
        form = layui.form;
        laydate = layui.laydate;

        form.render();

        tableInstance = table.render({
            id: 'loginLogTable',
            elem: '#loginLogTable',
            limits: [10, 15, 20],
            url: ServerURL + "/loginLog/list",
            headers: tokenUtil.getAuthHeader(),
            method: "POST",
            toolbar: '#toolbar',
            cols: [[
                {type: 'checkbox'},
                {field: 'loginId', title: '日志编号', align: 'center', minWidth: 100},
                {field: 'loginAccount', title: '登录账号', align: 'center', minWidth: 100},
                {field: 'loginIp', title: 'IP地址', align: 'center', minWidth: 100},
                {field: 'loginLocation', title: '登录地点', align: 'center', minWidth: 100},
                {field: 'browser', title: '浏览器', align: 'center', minWidth: 100},
                {field: 'os', title: '操作系统', align: 'center', minWidth: 100},
                {field: 'loginTime', title: '登录时间', align: 'center', minWidth: 100}
            ]],
            contentType: 'application/json',
            response: {
                statusName: 'code',
                statusCode: 200,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            },
            loading: true,
            page: true,
            error: function (res) {
                ajaxUtil.handleError(res);
            }
        });

        laydate.render({
            elem: '#loginTime',
            range: ['#loginBeginTime', '#loginEndTime']
        });

        table.on('toolbar(loginLogTable)', function (obj) {
            if (obj.event === 'delete') {
                deleteLoginLog();
            } else if (obj.event === 'truncate') {
                truncateLoginLog();
            }
        });

        cb();
    });
}

function initButtons() {
    $("#searchBtn").click(function () {
        reloadTable();
    });

    $("#resetBtn").click(function () {
        form.val('form', {
            "loginAccount": "",
            "loginBeginTime": "",
            "loginEndTime": ""
        });
        reloadTable();
    });
}

function reloadTable() {
    let data = form.val('form');
    if (data.loginBeginTime !== "") {
        data.loginBeginTime = data.loginBeginTime + " 00:00:00";
    }
    if (data.loginEndTime !== "") {
        data.loginEndTime = data.loginEndTime + " 23:59:59";
    }
    tableInstance.reload({
        where: data,
        page: {
            curr: 1
        }
    });
}

function deleteLoginLog() {
    let data = table.checkStatus('loginLogTable').data;
    let loginIds = [];
    data.forEach(function (item) {
        loginIds.push(item.loginId);
    });
    if (loginIds.length === 0) {
        layerUtil.error("请先选中一条记录");
        return false;
    }
    layerUtil.confirm("确定要删除记录么?", function () {
        ajaxUtil.simplePost({"loginIds": loginIds}, "/loginLog/deleteLoginLogs", function () {
            reloadTable();
        });
    });
}

function truncateLoginLog() {
    layerUtil.confirm("确定要清空记录么?", function () {
        ajaxUtil.simplePost({}, "/loginLog/truncate", function () {
            reloadTable();
        });
    });
}